const app = getApp()
import router from "@src/router";
import { addReadTimes } from '@src/api/pop'
import { _debounce } from '@src/common/utils'
import { dumpFunc } from "../../common/dump";
import {recordCustomerPop} from '@src/api/pop'
Component({
  properties: {
    // 弹窗的数据
    pops: {
      type: Object,
      value: {}
    },
    // 悬浮图是否隐藏
    showSuspend: {
      type: Boolean,
      value: true
    },
    // 弹窗图是否隐藏
    showAd: {
      type: Boolean,
      value: false
    },
    isMember: {
      type: Boolean,
      value: false
    },
    login: {
      type: Boolean,
      value: false
    }
  },
  data: {
    // 防止多次点击的初始值
    checked: false,
    timeId:null,
    baseUrl: "",
    // showSuspend: true,
    // showAd: false,
    mx: 10,
    my: 10,
    x: "600px",
    y: "",
    taBarHeight: 0,
    // token:$cache.get("token")
  },
  lifetimes: {
    ready() {
      this.setData({ baseUrl: getApp().globalData.extConfig.fileBaseUrl })
    },
  },
    
  methods: { 
    // 记录弹窗时间
    getPopTime() {
      if (pops.showRule > 0) {
        recordCustomerPop({popId:this.data.pops.popId}).then(res => {
          console.log(327,res);
        })
      }
    },
      // 去储值页面
    toStorePage: _debounce(async function (e) {
      this.setData({ checked: true })
      const index = e.currentTarget.dataset.index
      // 判断有没有登陆，登陆才可以跳，没登录引导登陆
      if (this.data.login) {
        router.push({
          path: e.currentTarget.dataset.item.path,
          params: {
            money:e.currentTarget.dataset.item.chargeLists[index][0] / 100
         }
        })
        
          // 查阅请求  进入页面就调用，（要防止多次点击）
        const res = await addReadTimes(this.properties.pops.id)
        if (res.code === 0) {
          this.setData({checked:true})
        }
        this.setData({checked:false})
      } else {
        this.selectComponent("#login").requireLogin()
         this.getPopTime()
      }
     
    }, 300),
    
     
    
      // 点击我要储值
    needStore: _debounce(async function (e) {
      this.setData({checked:true})
      if (this.data.login) {
          // 判断是否是会员
        if (this.properties.isMember) {
          router.push({
            path:'sp-member/pages/balanceRecharge/main'
          })
        } else {
          router.push({
            path:e.currentTarget.dataset.item.path
          })
        }
       // 查阅请求  进入页面就调用，（要防止多次点击）
        const res = await addReadTimes(this.properties.pops.id)
        if (res.code === 0) {
          this.setData({checked:true})
        }
        this.setData({checked:false})
      } else {
        this.selectComponent("#login").requireLogin()
        this.getPopTime()
      }
    },300),
      
    // 点击普通弹窗跳转
    toGeneralPop: _debounce(async function (e) {
      this.setData({ checked: true })
      if (this.data.login) {
        if (e.currentTarget.dataset.item.path === 'pages/index/main') {
          this.setData({
            showAd:true
          })
        }
        const {item}=e.currentTarget.dataset
        const dump = {
          appId: item.appId,
          feature:item.feature,
          featureParams:item.featureParams,
          insidePage: item.insidePage,
          insideParams: item.insideParams,
          path: item.path,
          type: item.type,
          url: item.url, 
        }
        dumpFunc(dump);
        
           // 查阅请求  进入页面就调用，（要防止多次点击）
         const res = await addReadTimes(this.properties.pops.id)
         if (res.code === 0) {
           this.setData({checked:true})
         }
         this.setData({checked:false})
      } else {
        this.selectComponent("#login").requireLogin()
        this.getPopTime()
      }
    }, 300),
    
      //cancelTap隐藏方向
      cancelTap() {
        this.triggerEvent('cancelPop')
      },
      cancelSuspend() {
        this.triggerEvent('cancelSm')
      },
      // 点击小图显示大图
      showAdImg() {
       this.triggerEvent('showAdImgEvent')
      },
   
      // 悬浮按钮拖动事件
      //拖动事件
      movableChange(event) {
        this.setData({
          mx: event.detail.x,
          my: event.detail.y
        })
      },
      //拖动失去事件
      touchendFun(event) {
        //获取屏幕宽度，自动靠边
        if (app.globalData.screenWidth / 2 > this.data.mx + 40) {
          this.setData({
            x: `0`,
            y: `${this.data.my}px`
          })
          console.log('X',this.data.x);
          console.log('Y',this.data.y);
        } else {
          this.setData({
            x: `${app.globalData.screenWidth}px - 52px`,
            y: `${this.data.my}px`
          })
        }
      },
    },
 })